<?php

class Employee extends AppModel
{

    var $name = 'Employee';
    var $displayField = 'firstname';
    var $order = array('Employee.firstname' => 'ASC');
    /*var $virtualFields = array(
        'Employee.name' => 'CONCAT(Employee.firstname, " ", Employee.lastname)'
    );*/
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'City' => array(
            'className' => 'City',
            'foreignKey' => 'city_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Employeetype' => array(
            'className' => 'Employeetype',
            'foreignKey' => 'employeetype_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Department' => array(
            'className' => 'Department',
            'foreignKey' => 'department_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Location' => array(
            'className' => 'Location',
            'foreignKey' => 'location_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    var $hasMany = array(
        'Potentialactivity' => array(
            'className' => 'Potentialactivity',
            'foreignKey' => 'employee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Useractivity' => array(
            'className' => 'Useractivity',
            'foreignKey' => 'employee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Potentialactivityassignedto' => array(
            'className' => 'Potentialactivity',
            'foreignKey' => 'assignedtoemployee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Useractivityassignedto' => array(
            'className' => 'Useractivity',
            'foreignKey' => 'assignedtoemployee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Potential' => array(
            'className' => 'Potential',
            'foreignKey' => 'employee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Potentialassignedto' => array(
            'className' => 'Potential',
            'foreignKey' => 'assignedtoemployee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Task' => array(
            'className' => 'Task',
            'foreignKey' => 'employee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Taskassignedto' => array(
            'className' => 'Task',
            'foreignKey' => 'assignedtoemployee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'employee_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    var $validate = array(
        'firstname' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'allowEmpty' => false,
                'required' => true,
                'message' => 'Please enter your first name'
            ),
            'alphaNumeric' => array(
                'rule' => 'alphaNumeric',
                'message' => 'Only letters and numbers are allowed in first name'
            ),
            'minLength' => array(
                'rule' => array('minLength', 2),
                'message' => 'First name should be atleast 2 characters long'
            )
        ),
        'lastname' => array(
            'minlength' => array(
                'rule' => array('minLength', 3),
                'allowEmpty' => true,
                'message' => 'Last name should be atleast 3 characters long'
            ),
            'alphaNumeric' => array(
                'rule' => 'alphaNumeric',
                'message' => 'Only letters and numbers are allowed in last name'
            )
        ),
        'primaryemail' => array(
            'email' => array(
                'rule' => 'email',
                'allowEmpty' => false,
                'required' => true,
                'message' => 'Please enter a valid email address'
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'This email has already been taken'
            )
        ),
        'password' => array(
            'minLength' => array(
                'rule' => array('minLength', 4),
                'message' => 'Password should be atleast 4 characters long'
            ),
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'allowEmpty' => false,
                'required' => true
            ),
            'equalTo' => array(
                'rule' => array('comparePasswords', array('retypePassword')),
                'message' => 'Passwords should be same. Please retype both password fields'
            )
        ),
        'retypePassword' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'allowEmpty' => false,
                'required' => true
            )
        ),
        'secondaryemail' => array(
            'email' => array(
                'rule' => 'email',
                'allowEmpty' => true,
                'message' => 'Please enter a valid email address'
            )
        ),
        'state_id' => array(
            'alphaNumeric' => array(
                'rule' => 'alphaNumeric',
                'allowEmpty' => false,
                'required' => true,
                'message' => 'Please choose your state'
            )
        ),
        'city_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'allowEmpty' => false,
                'required' => true,
                'message' => 'Please choose your city'
            )
        ),
        'location_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'allowEmpty' => false,
                'required' => true,
                'message' => 'Please choose your work location'
            )
        ),
        'employeetype_id' => array(
            'numeric' => array(
                'rule' => 'numeric',
                'allowEmpty' => false,
                'required' => true,
                'message' => 'Please choose your work role/designation'
            )
        ),
        'officephone1' => array(
            'alphaNumeric' => array(
                'rule' => 'alphaNumeric',
                'allowEmpty' => false,
                'required' => true,
                'message' => 'Please enter your office phone number'
            )
        ),
        'officephone2' => array(
            'alphaNumeric' => array(
                'rule' => 'alphaNumeric',
                'allowEmpty' => true,
                'message' => 'Please enter your office phone number'
            )
        ),
        'department_id' => array(
            'alphaNumeric' => array(
                'rule' => 'alphaNumeric',
                'allowEmpty' => false,
                'required' => true,
                'message' => 'Please choose your department'
            )
        )
    );

    /**
     * compare $value with the $options element
     *
     * @param Array $value The name of the field in which this rule is called
     * @param Array $options The fields with which value in $value be matched
     * @param <type> $rule
     * @return Boolean
     */
    public function comparePasswords($value = array(), $options = array(), $rule = array())
    {
        if( !$this->compare($value, Security::hash($this->data[$this->alias][$options[0]], 'sha1', true)) )
        {
            $this->invalidate($options[0], $rule['message']);
            return false;
        }
        return true;
    }

}
?>